package me.qi.kancha.controller;

import me.qi.kancha.controller.base.BaseController;
import me.qi.kancha.dto.core.ArticleDTO;
import me.qi.kancha.service.ArticleService;
import me.qi.kancha.service.base.support.PageData;
import me.qi.kancha.vo.article.ArticleInsertVo;
import me.qi.kancha.vo.article.ArticleQueryVo;
import me.qi.kancha.vo.article.ArticleStatusChangeVo;
import me.qi.kancha.vo.article.ArticleUpdateVo;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章表模块
 * 
* @author qi
* @date 2022-10-26 01:43:51
* @modified By：
*/
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController {

    private ArticleService service;

    public ArticleController(ArticleService service) {
        this.service = service;
    }

    /**
     * 文章表模块-分页列表
     *
     * @param param
     * @return
     */
    @GetMapping
    public PageData<ArticleDTO> listPaging(@Validated ArticleQueryVo param, BindingResult result) {
        //请根据需求重写定义入参, 并在 serviceImpl 冲重写 listPaging 方法, 通过强转获得真正的入参类型
        checkBindingResult(result);
        return service.listPaging(param);
    }

    /**
     * 文章表模块-新增
     *
     * @param param
     * @return
     */
    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public ArticleDTO insert(@Validated @RequestBody ArticleInsertVo param, BindingResult result) {
        //请根据需求重写定义入参, 并在 serviceImpl 冲重写 insert 方法, 通过强转获得真正的入参类型
        checkBindingResult(result);
        return service.insert(param);
    }

    /**
     * 文章表模块-修改
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ArticleDTO update(@Validated @RequestBody ArticleUpdateVo param, BindingResult result) {
        //请根据需求重写定义入参, 并在 serviceImpl 冲重写 update 方法, 通过强转获得真正的入参类型
        checkBindingResult(result);
        return service.update(param);
    }

    /**
     * 文章表模块-批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping(value = "/deleteBatch", consumes = MediaType.APPLICATION_JSON_VALUE)
    public void deleteBatch(@RequestBody List<String> ids) {
        //如果有其他关联业务-在serviceImpl中重写 removeBatchByIds 方法
        service.removeBatchByIds(ids);
    }

    /**
     * 文章表模块-删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id:" + ID_REGX + "}")
    public void delete(@PathVariable String id) {
        //如果有其他关联业务-在 serviceImpl 中重写 removeById 方法
        service.removeById(id);
    }

    /**
     * 文章表模块-详情
     *
     * @return
     */
    @GetMapping(value = "/{id:" + ID_REGX + "}")
    public ArticleDTO detail(@PathVariable String id) {
        // 默认方法只根据id查询单表数据。如果接口需要返回关联表数据：
        // 1. 根据接口数据需求定义集成DTO的VO类
        // 2. serviceImpl重写getById 并返回VO 类
        // 3. 在controller 将结果强转成VO类返回给前端
        return service.getById(id);
    }

    /**
     * 档案模块-批量操作（提交&撤回）
     *
     * @param ids
     * @return
     */
    @PostMapping(value = "/statusChange", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity statusChange(@RequestBody ArticleStatusChangeVo param) {
        boolean b = service.statusChange(param);
        return build(b, "操作失败");
    }

}
